//
//  HCComposeViewController.swift
//  Hweibome
//
//  Created by apple on 15/12/15.
//  Copyright © 2015年 hechuan. All rights reserved.
//

import UIKit

class HCComposeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
//        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Done, target: self, action: "back")
    }
    
    @objc private func back() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
  private  func setupUI(){
        setupNav()
        view.backgroundColor = UIColor.whiteColor()
        view.addSubview(textView)
        if #available(iOS 9.0, *) {
        view.addSubview(toolBar)
        } else {
            // Fallback on earlier versions
        }
    
    if #available(iOS 9.0, *) {
        toolBar.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self.view)
            make.bottom.equalTo(self.view)
            make.height.equalTo(44)
        }
    } else {
        // Fallback on earlier versions
    }
    textView.snp_makeConstraints { (make) -> Void in
        make.edges.equalTo(view).offset(UIEdgeInsetsZero)
    }
      //设置属性
    toolBar.childButtonClickClosure = { [weak self]     ////循环引用????,不太理解那个和那个互相引用
        (type: HCComposeToolBarButtonType) ->() in
        switch type {
        case .Picture:
            print("1")
            self?.selectedPicture() //子闭包里调用本类的方法,需要加self
        case .Mention:
            print("2")
        case .Trend:
            print("3")
        case .Emotion:
            print("4")
        case .Add:
            print("5")
        }
    }
    
}
    
    private func setupNav(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", target: self, action: "back")
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sendButton)
        navigationItem.rightBarButtonItem?.enabled = false
        navigationItem.titleView = titleView
    }
    
    private lazy var titleView: UILabel = {
        let titleView = UILabel()
        if let name = HCUserAccountViewModel.sharedAccount.userAccount?.screen_name {
            titleView.numberOfLines = 0
            let attrText = NSMutableAttributedString(string: "发微博\n\(name)")
            let range = (attrText.string as NSString).rangeOfString(name)
            let attrs = [
                NSFontAttributeName: UIFont.systemFontOfSize(14),
                NSForegroundColorAttributeName: UIColor.grayColor()
            ]
            attrText.addAttributes(attrs,range: range)
            titleView.attributedText = attrText
        } else {
            titleView.text = "发微博"
        }
        titleView.textAlignment = .Center
        titleView.sizeToFit()
        return titleView
    }()
    
  

    
    private lazy var sendButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_orange"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Disabled)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Disabled)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitle("发送", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.size = CGSize(width: 45, height: 30)
        return button
    }()
    //toolbar
    @available(iOS 9.0, *)
    private lazy var toolBar:HCComposeToolBar = HCComposeToolBar(frame: CGRectZero)
    
    private lazy var textView: HCComposeTextView = {
       let textView = HCComposeTextView()
        textView.font = UIFont.systemFontOfSize(16)
        textView.placeholder = "我是占位文字，请在这里输入你想表达的内容"
        textView.delegate = self
        return textView
    }()
}

extension HCComposeViewController {
    private func selectedPicture(){
        let pickerVC = UIImagePickerController()
        let result = UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary)
      //  UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Rear)
        pickerVC.allowsEditing = true
        
        if result {
            presentViewController(pickerVC, animated: true, completion: nil)
        }else {
            printLog("照相机不可用")
        }
    }
}

//MARK: - UITextViewDelegate
extension HCComposeViewController:UITextViewDelegate {
    func textViewDidChange(textView: UITextView){
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
}
